package com.kotlin.myapplication

import android.content.res.Configuration
import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.LocalIndication
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsPressedAsState
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.kotlin.myapplication.ui.theme.BgButtonDark
import com.kotlin.myapplication.ui.theme.BgColumn
import com.kotlin.myapplication.ui.theme.Black
import com.kotlin.myapplication.ui.theme.MyApplicationTheme
import com.kotlin.myapplication.ui.theme.Teal200
import com.kotlin.myapplication.ui.theme.White

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Log.e("huang","${resources.configuration.colorMode}")
        }
        orientation = resources.configuration.orientation == 1
        setContent {
            MyApplicationTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    Greeting()
                }
            }
        }
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        orientation = newConfig.orientation == 1
    }
}
var orientation by mutableStateOf(true)

val buttons = arrayOf(
    arrayOf(
        "C" to Teal200,
        "0" to Black,
        "+" to Teal200,
        "-" to Teal200
    ),
    arrayOf(
        "1" to Black,
        "2" to Black,
        "3" to Black,
        "x" to Teal200
    ),
    arrayOf(
        "4" to Black,
        "5" to Black,
        "6" to Black,
        "/" to Teal200
    ),
    arrayOf(
        "7" to Black,
        "8" to Black,
        "9" to Black,
        "=" to Teal200
    )
)
val buttonsHor = arrayOf(
    arrayOf(
        "1" to Black,
        "2" to Black,
        "3" to Black,
        "4" to Black,
        "5" to Black,
        "+" to Teal200,
        "-" to Teal200,
        "C" to Teal200,
    ),
    arrayOf(
        "6" to Black,
        "7" to Black,
        "8" to Black,
        "9" to Black,
        "0" to Black,
        "x" to Teal200,
        "/" to Teal200,
        "=" to Teal200
    ),
)

data class CalculateState(val first: Int = 0,val second:Int = 0,val opt:String? = "")

fun calculate(state: CalculateState,input:String):CalculateState{
    return when(input){
        in "0".."9" -> state.copy(second = input.toInt(), first = state.second)
        in arrayOf("+","-","x","/") -> state.copy(opt = input)
        "=" -> when(state.opt){
            "+" -> state.copy(second = state.first + state.second)
            "-" -> state.copy(second = state.first - state.second)
            "x" -> state.copy(second = state.first * state.second)
            "/" -> {
                if (state.second == 0){
                    state.copy(first = 0, second = 0,opt = "")
                }else{
                    state.copy(second = state.first / state.second)
                }
            }
            else -> state
        }
        else -> {
            state.copy(first = 0, second = 0,opt = "")
        }
    }

}

@Composable
fun Greeting() {
    val paddingValues = 10.dp
    var state by remember {
        mutableStateOf(CalculateState())
    }
    Column() {
        Box(
            Modifier.weight(1f)
                .fillMaxWidth()
                .padding(paddingValues),
            contentAlignment = Alignment.BottomEnd
        ) {
            Text(
                text = "${state.second}",
                fontSize = 100.sp
            )
        }
        Column(
            Modifier.aspectRatio(if (orientation) 1f else 4f)
                .clip(
                    RoundedCornerShape(
                        paddingValues,
                        paddingValues,
                        0.dp,
                        0.dp
                    )
                )
                .background(BgColumn)
                .padding(
                    paddingValues,
                    paddingValues,
                    paddingValues,
                    0.dp
                )
        ){
            if(orientation)
                buttons
            else {
                buttonsHor
            }.forEach {
                Row(
                    Modifier.weight(1f),
                    horizontalArrangement = Arrangement.spacedBy(paddingValues)
                ) {
                    it.forEach {
                        CustomButton(
                            it,
                            Modifier.weight(1f)
                                .aspectRatio(1f))
                        {
                            state = calculate(state,it.first)
                        }
                    }
                }
            }
        }
    }
}

@Composable
fun CustomButton(textInfo:Pair<String,Color>,modifier: Modifier,click: () -> Unit = {}){
    val interaction = remember { MutableInteractionSource() }
    val isPressed by interaction.collectIsPressedAsState()
    Box(
        modifier.shadow(5.dp, CircleShape)
            .border(1.dp, BgButtonDark, CircleShape)
            .background(White)
            .clickable(
                interactionSource = interaction,
                indication = LocalIndication.current
            ) {
                click.invoke()
              },
        contentAlignment = Alignment.Center
    ){
        Text(
            text = textInfo.first,
            fontSize = if(isPressed) 50.sp else 30.sp,
            color = textInfo.second,
            textAlign = TextAlign.Center
        )
    }

}

@Preview(showSystemUi = true)
@Composable
fun DefaultPreview() {
    MyApplicationTheme {
        Greeting()
    }
}